
package guoan.service;

import guoan.domain.Adv;
import guoan.domain.AdvPay;
import guoan.domain.Keyword;
import guoan.repository.KeywordRepository;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = false)
public class KeywordService {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private KeywordRepository keywordRepository;
 

    public void add(String keyName, String keyNameInfo, Long id){

        String[] kws = keyName.trim().split(" ");
        for (String kw : kws) {
            // TODO Auto-generated method stub
            if (!kw.equals("")) {
                Keyword keyword = new Keyword();
                keyword.setKeyInfo(keyNameInfo);
                keyword.setKeyInfoId(id);
                keyword.setKeyName(kw);
                keywordRepository.save(keyword);
            }
        }
    }

    public Keyword update(Keyword keyword){
        // TODO Auto-generated method stub
        return keywordRepository.save(keyword);
    }

    public void delete(String keyName_info, Long id){
        // TODO Auto-generated method stub
        List<Keyword> keywords = keywordRepository.findByKeyInfoAndKeyInfoId(
                keyName_info, id);
        keywordRepository.delete(keywords);
    }

    public Keyword findTop1ByKeyName(String keyName){
        // TODO Auto-generated method stub
        return keywordRepository.findTop1ByKeyName(keyName);
    }

    public List<Keyword> findTop9ByKeyName(String keyName){
        // TODO Auto-generated method stub
        return keywordRepository.findTop9ByKeyName(keyName);
    }


}
